package com.whq.security.context;

public class InheritableThreadLocalDataContextHolderStrategy implements DataContextHolderStrategy{

    private static final ThreadLocal<Object> contextHolder = new InheritableThreadLocal<>();

    @Override
    public void clearContext() {
        contextHolder.remove();
    }

    @Override
    public Object getContext() {
        Object ctx = contextHolder.get();
        if (ctx == null) {
            ctx = createEmptyContext();
            contextHolder.set(ctx);
        }
        return ctx;
    }

    @Override
    public void setContext(Object context) {
        contextHolder.set(context);
    }

    @Override
    public Object createEmptyContext() {
        return new Object();
    }
}
